<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="boxCode">
              <el-input v-model="filters.boxCode" clearable placeholder="标本箱编号"></el-input>
            </el-form-item>
            <el-form-item prop="boxName">
              <el-input v-model="filters.boxName" clearable placeholder="标本箱名称"></el-input>
            </el-form-item>
            <el-form-item prop="state">
              <el-select v-model="filters.state" :clearable="true" placeholder="标本箱状态">
                <el-option v-for="item in dataStatue" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="boxName" label="标本箱名称" align="center"></el-table-column>
        <el-table-column prop="boxCode" label="标本箱编号" align="center"></el-table-column>
        <el-table-column prop="boxMac" label="MAC地址" align="center"></el-table-column>
        <el-table-column prop="boxType" label="标本箱类型" width="120" align="center">
          <template slot-scope="{ row }">
            <div>
              {{ row.boxType == 1 ? '一代' : '二代' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="waiterName" label="服务员" align="center"></el-table-column>
        <el-table-column prop="state" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatBoxStatuss(scope.row.boxStatus)">
                {{ formatBoxStatus(scope.row.boxStatus) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="{ row }">
            <el-tag effect="dark" :type="$Filters.formatStates(row.state)" size="mini">
              {{ $Filters.formatState(row.state) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="180" align="center"></el-table-column>
        <el-table-column label="操作" width="136" align="center">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{ scope.row.state === 0 ? '启用' : '禁用' }}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state === 0" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import SpecimenApi from '@/api/equip/specimen';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';

export default {
  name: 'SpecimenManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      filters: {
        boxCode: '',
        boxName: '',
        state: null,
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      dataStatue: [
        { label: '禁用', value: '0' },
        { label: '启用', value: '1' },
      ],

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_DELETE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  watch: {
    //to去哪，from从哪来
    $route(to, from) {
      if (from.path == '/equip/specimenEdit' || from.path == '/equip/specimenAdd') {
        this.listData();
      }
    },
  },
  methods: {
    init() {
      let _this = this;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.listData();
    },
    formatBoxStatus(type) {
      switch (type) {
        case 0:
          return '未连接';
        case 1:
          return '已连接';
        case 2:
          return '已断开';
        case 3:
          return '已休眠';
      }
    },
    formatBoxStatuss(cellValue) {
      if (cellValue === 0) {
        return 'primary';
      } else if (cellValue === 1) {
        return 'success';
      } else if (cellValue === 2) {
        return 'danger';
      } else if (cellValue === 3) {
        return 'info';
      }
    },
    formatPower(row, column, cellValue) {
      // 电量警告
      if (cellValue === '' || cellValue === null) {
        return '未知';
      } else if (cellValue <= 10) {
        return cellValue + '-电量低';
      } else if (cellValue > 10 || value <= 100) {
        return cellValue;
      }
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/equip/specimenAdd');
    },
    handleClickEditor(row) {
      this.$router.push({
        path: '/equip/specimenEdit',
        query: { id: row.boxId },
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/specimenDetail',
        query: { id: row.boxId },
      });
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        SpecimenApi.deleteSpecimen({
          id: row.boxId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        SpecimenApi.deleteSpecimen({
          id: val.boxId,
          state: -1,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
      };
      let boxName = _this.filters.boxName;
      let boxCode = _this.filters.boxCode;
      let state = _this.filters.state;
      if (boxName !== undefined && boxName !== null && boxName !== '') {
        cond.boxName = boxName;
      }
      if (boxCode !== undefined && boxCode !== null && boxCode !== '') {
        cond.boxCode = boxCode;
      }
      if (state !== undefined && state !== null && state !== '') {
        cond.state = state;
      }
      SpecimenApi.listSpecimen(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
